//
//  MyCarLocatinViewController.m
//  edgeDemo
//
//  Created by dllo on 16/3/8.
//  Copyright © 2016年 徐宏飞. All rights reserved.
//

#import "MyCarLocatinViewController.h"
#import "XLImagePageView.h"

#define MM 128


@interface MyCarLocatinViewController ()
<
UIScrollViewDelegate,
UINavigationControllerDelegate,
UIImagePickerControllerDelegate,
XLImagePageViewDelegate
>

@property (nonatomic, strong) UIScrollView *myScrollView;
@property (nonatomic, strong) XLImagePageView *xlImagePageView;

@end

@implementation MyCarLocatinViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.110 green:0.102 blue:0.024 alpha:1.000];;
    
    //导航条区域
    [self.navigationController setNavigationBarHidden:NO animated:TRUE];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navMyCar_BG"] forBarMetrics:UIBarMetricsDefault];
    
    UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    label.text = @"爱车位置";
    label.textColor = WHITE;
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = CLEAR;
    self.navigationItem.titleView = label;
    
    
    //创建左右按钮
    [self createLeftAndRightButton];
    
    
    //数据源
    NSMutableArray *array = [self getDataSource];
    

    //view
    self.xlImagePageView = [[XLImagePageView alloc] initWithFrame:CGRectMake(0, 0, 414, 736-128)];
    self.xlImagePageView.imageArray = array;
    self.xlImagePageView.delegate = self;
    [self.view addSubview:self.xlImagePageView];
    
   
    
}



#pragma mark -- 创建左右按钮
- (void)createLeftAndRightButton{
    //左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"barbuttonicon_back_cube@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = WHITE;
    
    //右按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"再拍一张" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    self.navigationItem.rightBarButtonItem.tintColor = WHITE;
 
    //完成找车
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(8, 616, 389, 48);
    button.backgroundColor = [UIColor colorWithRed:0.212 green:0.455 blue:0.878 alpha:1.000];
    [button setTitle:@"完成找车(清除图片)" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(clearButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
}



#pragma mark -- 左按钮执行方法
- (void)leftButtonAction:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 右按钮执行方法
- (void)rightButtonAction:(UIButton *)btn{
    NSLog(@"再拍一张");
    [self showImagePickController];
}

#pragma mark -- 完成找车钮执行方法
- (void)clearButtonClick:(UIButton *)btn{
    NSLog(@"完成找车");
    //覆盖数据
    NSString *libPath = [LIBRARY stringByAppendingString:@"/Caches"];
    NSString *path = [libPath stringByAppendingFormat:@"/bookmark.plist"];
    NSMutableArray *array = [NSMutableArray array];
    BOOL result = [array writeToFile:path atomically:YES];
    if (result) {
        NSLog(@"覆盖成功%@", path);
    } else {
        NSLog(@"覆盖失败");
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark --调取系统相册
-(void)showImagePickController
{
    UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
    //  选择类型 （照片库模式， 相机模式， 相机胶片模式）
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imagePicker.delegate = self;//代理
    imagePicker.allowsEditing = YES;//是否允许编辑
    // 模态，进入相册
    [self presentViewController:imagePicker animated:YES completion:^{
        
    }];
    
}

#pragma mark --选中相片后会执行
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *libPath = [LIBRARY stringByAppendingString:@"/Caches"];
    NSString *path = [libPath stringByAppendingFormat:@"/bookmark.plist"];
    NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path];
    //获取到选中的照片 并显示 并赋值给view的属性headImageView
    UIImage *image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
    NSData *imageData = UIImagePNGRepresentation(image);
    [array addObject:imageData];
    
    BOOL result = [array writeToFile:path atomically:YES];
    if (result) {
        NSLog(@"写入成功%@", path);
    } else {
        NSLog(@"写入失败");
    }
    
    //从新获取数据(刷新scrollView)
    NSMutableArray *dataArr = [self getDataSource];
    self.xlImagePageView.imageArray = dataArr;
    
    
    // 通过模态返回
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
    
}

#pragma mark --取消后会执行
-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    // 通过模态消失
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
}

#pragma mark -- 获取数据
- (NSMutableArray *)getDataSource{
    NSString *libPath = [LIBRARY stringByAppendingString:@"/Caches"];
    NSString *path = [libPath stringByAppendingFormat:@"/bookmark.plist"];
    NSMutableArray *newArr = [NSMutableArray arrayWithContentsOfFile:path];
    NSMutableArray *array = [NSMutableArray array];
    for (NSData *data in newArr) {
        UIImage *image = [UIImage imageWithData:data];
        [array addObject:image];
    }
    return array;
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
